
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author f0899
 */
public class Teht1b extends JApplet implements Runnable {

    private Thread saie;
    private String viesti;
    private Font f1;
    private FontMetrics fontinMitat;
    private int x, y;

    @Override
    public void init() {
        this.saie = null;
        viesti = "Pärr pärr!";
        f1 = new Font("verdana", Font.BOLD, 20);
        x = getSize().width; //appletin leveys
        y = getSize().height / 2; //puolivälissä korkeudesta
        getContentPane().add(new PiirtoPaneeli());
    }

    public class PiirtoPaneeli extends JPanel {

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setFont(f1);
            g.setColor(Color.red);
            g.drawString(viesti, x, y);
            fontinMitat = g.getFontMetrics();
            x = x - 5;
            if (x <= fontinMitat.stringWidth(viesti) * -1) {
                x = getSize().width; //appletin leveys;
            }
        }
    }

    @Override
    public void start() {
        if (saie == null) {
            saie = new Thread(this);
            saie.start();
        }
    }

    @Override
    public void run() {
        while (true) {
            repaint();
            try {
                saie.sleep(10);
            } catch (InterruptedException e) {
            }
        }
    }
}
